/**@description  数据格式化 */
export function traverse(instance, ret = []) {
  ret.name =
    instance.constructor.options.__file ||
    instance.constructor.options.name ||
    instance.constructor.name;

  setType(ret);
  setFile(ret);

  ret.instance = instance;

  if (instance.$children && instance.$children.length > 0) {
    ret.children = [];
    instance.$children.forEach((instanceItem) => {
      const rs = {};
      traverse(instanceItem, rs);
      ret.children.push(rs);
    });
  }
}
// el-tag的类型
const mapping = {
  layout: "success",
  components: "warning",
  views: "danger",
};
/**@description 设置类型 */
function setType(item) {
  if (item.name.startsWith("src/")) {
    const fileName = item.name.split("/")[1];
    if (fileName.endsWith(".vue")) {
      item.type = fileName.slice(-4);
    } else {
      item.type = fileName;
    }
  }
  if (mapping[item.type]) {
    item.typeTag = mapping[item.type];
  }
}
/**@description 设置文件 */
function setFile(item) {
  if (item.name.startsWith("src/")) {
    item.file = item.name;
  }
}
